﻿Imports ACompany.OrderProcessing.Model
Imports ACompany.OrderProcessing.Services
Imports StructureMap

Public Class CustomerListPresenter

    Private _view As ICustomerListView
    Private _CustomerService As ICustomerService

    Public Sub New(ByVal view As ICustomerListView)
        Me.New(view, ObjectFactory.GetInstance(Of ICustomerService)())
    End Sub

    Public Sub New(ByVal view As ICustomerListView, ByVal CustomerService As ICustomerService)
        Me._view = view
        Me._CustomerService = CustomerService
    End Sub

    Public Sub Display()

        clearInfoAndErrorMessages()

        Dim AllCustomers As List(Of Customer)

        AllCustomers = _CustomerService.FindAll

        Me._view.Customers = AllCustomers
    End Sub

    Public Sub RemoveCustomerBy(ByVal CustomerId As Guid)

        clearInfoAndErrorMessages()

        Dim Customer As Customer
        Customer = _CustomerService.FindBy(CustomerId)

        If Not Customer Is Nothing Then
            If Customer.Orders.Count = 0 Then
                _CustomerService.Remove(Customer)
                Display()
            Else
                Me._view.ErrorMessage("Cannot delete a customer that has orders")
            End If
        Else
            ' Update error view
            Me._view.ErrorMessage(String.Format("A customer with an Id of '{}' cannot be found", CustomerId))
        End If

    End Sub

    Private Sub clearInfoAndErrorMessages()
        _view.ErrorMessage(String.Empty)
        _view.InfoMessage(String.Empty)
    End Sub

End Class
